package im.yisme.controler;

import android.util.Log;
import android.view.MotionEvent;
import im.yisme.controler.service.RecordCtrlService;
import im.yisme.model.service.ModelServiceProvider;
import im.yisme.model.service.RecordSystemService;
import im.yisme.view.service.RecordViewService;
import im.yisme.vo.MusicVO;
import im.yisme.vo.RawActionVO;
import im.yisme.vo.InputResultVO;

public class RecordCtrl implements RecordCtrlService{

	private static RecordCtrl ctrl;
	private RecordSystemService service;
	private boolean quitFlag;
	private RecordViewService view;
	
	private RecordCtrl(){
		service = ModelServiceProvider.getRecordSystemService();
	}
	
	public static RecordCtrl getInstance(){
		return ctrl == null ? ctrl = new RecordCtrl() : ctrl;
	}
	
	
	public void setRecordingMusic(long musicId) {
		// TODO Auto-generated method stub
		MusicCtrl musicCtrl = MusicCtrl.getInstance();
		MusicVO recordingMusic = musicCtrl.findMusic(musicId);
		
		service.setRecordingMusic(recordingMusic);
	}

	
	public void loadResource() {
		// TODO Auto-generated method stub		
		service.loadResource();			
	}


	public void startRecord() {
		// TODO Auto-generated method stub							
	}

	public void reset() {
		// TODO Auto-generated method stub
		
		service.reset();
		
	}


	public void quit() {
		// TODO Auto-generated method stub
		service.reset();
	}
	
	public void finishRecord() {
		// TODO Auto-generated method stub
		service.finishRecord();
	}

	public void input(RawActionVO action) {
		// TODO Auto-generated method stub
		InputResultVO result = service.handleInput(action);
		
		feedbackToView(result);
	}

	public void setView(RecordViewService view) {
		// TODO Auto-generated method stub
		this.view = view;
	}
	
	private void feedbackToView(InputResultVO result){
		if(result == null) return;

		switch (result.getAction()) {
		case RawActionVO.UP:
			
			if(result.isSuccess()) {
				int[] points = result.getPoints();
				
				if(points != null)
					view.comet(points, System.currentTimeMillis()+500, true);
				else 
					view.validTap(result.getWhichPillar());			
			}
			break;


		default:
			break;
		}
	}


	


	
}
